/*
 * @Author: your name
 * @Date: 2021-03-11 15:58:32
 * @LastEditTime: 2021-03-15 15:00:16
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \wx_calander_app\lib\pages\evaPage\widget\eva_banner.dart
 */

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'decoration.dart';

var start = Offset(0, 446.w);

var controllPoint = Offset(650.w, 446.w);

var endPoint = Offset(750.w, 446.w - 160.w);

class EvaBanner extends StatefulWidget {
  final evaTime;
  final score;
  EvaBanner({this.evaTime, this.score});
  @override
  _EvaBannerState createState() => _EvaBannerState();
}

class _EvaBannerState extends State<EvaBanner> {
  Map<String, dynamic> currentStudent = {};
  String level = '良好';
  @override
  void initState() {
    super.initState();
    if (mounted) {
      if (widget.score < 40) {
        setState(() {
          level = '待加强';
        });
      } else if (widget.score < 60) {
        setState(() {
          level = '一般';
        });
      } else if (widget.score <= 85) {
        setState(() {
          level = '良好';
        });
      }
    } else {
      setState(() {
        level = '优秀';
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    double score = widget.score;
    return Stack(
      children: [
        ClipPath(
            clipper: MyClipper(),
            child: Container(
                height: 446.w,
                width: MediaQuery.of(context).size.width,
                decoration: BoxDecoration(
                  image: new DecorationImage(
                    fit: BoxFit.cover,
                    image: new AssetImage(
                      'images/eva_banner.png',
                    ),
                  ),
                ))),
        CustomPaint(
            painter: CurvePainter1(score, Colors.white, true),
            child: Container(
              height: 445.w,
              width: MediaQuery.of(context).size.width,
            )),
        ClipPath(
          clipper: MyProcessClipper(score),
          child: CustomPaint(
              painter: CurvePainter1(score, HexColor("#FFA900"), false),
              child: Container(
                height: 446.w,
                width: MediaQuery.of(context).size.width,
              )),
        ),
        Positioned(
            top: 44.w,
            left: 40.w,
            child: Container(
              width: 690.w,
              child: Text(
                '家庭环境适宜度报告',
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.bold,
                    fontSize: 44.sp),
              ),
            )),
        Positioned(
            top: 108.w,
            left: 42.w,
            child: Container(
              alignment: Alignment.center,
              width: 268.w,
              height: 42.w,
              decoration: DecorationWidege.decation(
                  Colors.white.withOpacity(0.2), 21.w),
              child: Text(
                '评估时间：${widget.evaTime.split(' ')[0]}',
                style: TextStyle(color: Colors.white, fontSize: 22.sp),
              ),
            )),
        Positioned(
            top: 219.w,
            left: 42.w,
            child: Container(
              child: Text(
                '家庭环境适宜度指数',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 24.sp,
                  fontWeight: FontWeight.bold,
                ),
              ),
            )),
        Positioned(
            top: 257.w,
            left: 42.w,
            child: Container(
                height: 70.w,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Text(
                      widget.score.toStringAsFixed(0).toString(),
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 70.sp,
                      ),
                    ),
                    SizedBox(
                      width: 10.w,
                    ),
                    Text(
                      level,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 24.sp,
                      ),
                    ),
                  ],
                ))),
        Positioned(
          left: twoBezier(0.25, start, controllPoint, endPoint).dx - 40.w,
          top: twoBezier(0.25, start, controllPoint, endPoint).dy - 120.w,
          child: LevelItem(level: '一般', score: '40'),
        ),
        Positioned(
          left: twoBezier(0.4, start, controllPoint, endPoint).dx - 40.w,
          top: twoBezier(0.4, start, controllPoint, endPoint).dy - 120.w,
          child: LevelItem(level: '良好', score: '60'),
        ),
        Positioned(
          left: twoBezier(0.69, start, controllPoint, endPoint).dx - 40.w,
          top: twoBezier(0.69, start, controllPoint, endPoint).dy - 120.w,
          child: LevelItem(level: '优秀', score: '85'),
        )
      ],
    );
  }
}

class LevelItem extends StatelessWidget {
  String level;
  String score;
  LevelItem({this.level, this.score});
  @override
  Widget build(BuildContext context) {
    return Container(
        width: 80.w,
        height: 100.w,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              level,
              style: TextStyle(
                color: Colors.white,
                fontSize: 18.sp,
              ),
            ),
            SizedBox(
              height: 8.w,
            ),
            Text(
              score,
              style: TextStyle(
                color: Colors.white,
                fontSize: 16.sp,
              ),
            )
          ],
        ));
  }
}

/**
     * @desc 二阶贝塞尔
     * @param {number} t 当前百分比
     * @param {Array} p1 起点坐标
     * @param {Array} p2 终点坐标
     * @param {Array} cp 控制点
     */
twoBezier(t, Offset p1, Offset cp, Offset p2) {
  double x =
      (1 - t) * (1 - t) * p1.dx + 2 * t * (1 - t) * cp.dx + t * t * p2.dx;
  double y =
      (1 - t) * (1 - t) * p1.dy + 2 * t * (1 - t) * cp.dy + t * t * p2.dy;
  return Offset(x, y);
}

class MyProcessClipper extends CustomClipper<Path> {
  double score;
  MyProcessClipper(this.score);
  @override
  Path getClip(Size size) {
    var path = new Path();
    path.lineTo(size.width, size.height - 160.w);
    double angle = 0.0;
    if (score < 40) {
      angle = score / 40 * 0.25;
    } else if (score < 60) {
      angle = (score - 40.0) / 20 * 0.15 + 0.25;
    } else if (score < 85) {
      angle = (score - 60.0) / 20 * 0.23 + 0.4;
    } else {
      angle = (score - 85.0) / 20 * 0.31 + 0.69;
    }
    Offset point =
        twoBezier(angle, Offset(0, size.height), controllPoint, endPoint);
    path.lineTo(0, 0);
    path.lineTo(point.dx + 15.w, 0);
    path.lineTo(point.dx + 15.w, size.height + 10);
    path.lineTo(0, size.height + 10);

    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}

class MyClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    var path = new Path();
    path.lineTo(0, size.height);

    //var controllPoint = Offset(size.width - 100.w, size.height);

    //  var endPoint = Offset(size.width, size.height - 160.w);
    path.quadraticBezierTo(
        controllPoint.dx, controllPoint.dy, endPoint.dx, endPoint.dy);
    path.lineTo(size.width, 0);
    path.lineTo(0, 0);

    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}

class CurvePainter1 extends CustomPainter {
  double score;
  Color color;
  bool haveBool;
  CurvePainter1(this.score, this.color, this.haveBool);

  ///实际的绘画发生在这里
  @override
  void paint(Canvas canvas, Size size) {
    drawCircle({Paint paint, double radiu, double ang}) async {
      canvas.drawCircle(
          twoBezier(ang, Offset(0, size.height), controllPoint, endPoint),
          radiu,
          paint);
    }

    ///创建画笔
    var paint = Paint();

    ///设置画笔的颜色
    paint.strokeWidth = 8.w;
    double angle = 0.0;

    paint.style = PaintingStyle.stroke;
    paint.color = HexColor("#FFA900");

    var paint1 = Paint();
    var paint2 = Paint();
    var paint3 = Paint();
    if (score < 40) {
      angle = score / 40 * 0.25;
      paint1.color = Colors.white;
      paint2.color = Colors.white;
      paint3.color = Colors.white;
    } else if (score == 40) {
      angle = 0.25;
      paint1.color = Colors.white;
      paint2.color = Colors.white;
      paint3.color = Colors.white;
    } else if (score < 60) {
      angle = (score - 40.0) / 20 * 0.15 + 0.25;
      paint1.color = HexColor("#FFA900");
      paint2.color = Colors.white;
      paint3.color = Colors.white;
    } else if (score == 60) {
      angle = 0.4;
      paint1.color = HexColor("#FFA900");
      paint2.color = Colors.white;
      paint3.color = Colors.white;
    } else if (score < 85) {
      angle = (score - 60.0) / 20 * 0.23 + 0.4;
      paint1.color = HexColor("#FFA900");
      paint2.color = HexColor("#FFA900");
      paint3.color = Colors.white;
    } else if (score == 85) {
      angle = 0.69;
      paint1.color = HexColor("#FFA900");
      paint2.color = HexColor("#FFA900");
      paint3.color = Colors.white;
    } else {
      angle = (score - 85.0) / 20 * 0.31 + 0.69;
      paint1.color = HexColor("#FFA900");
      paint2.color = HexColor("#FFA900");
      paint3.color = HexColor("#FFA900");
    }

    paint1.style = PaintingStyle.fill;
    paint2.style = PaintingStyle.fill;
    paint3.style = PaintingStyle.fill;

    ///创建路径
    var path = Path();
    path.moveTo(0, size.height);
    path.quadraticBezierTo(
      controllPoint.dx,
      controllPoint.dy,
      endPoint.dx,
      endPoint.dy,
    );

    paint.color = color;
    if (haveBool == false) {
      final Gradient gradient = SweepGradient(
        startAngle: 0,
        endAngle: 1,
        colors: [HexColor("#FFD700"), HexColor("#FF7000")],
      );
      final blockSize =
          Rect.fromCircle(center: Offset(0.w, 148.w), radius: 5.w);
      paint.shader = gradient.createShader(blockSize);
    } else {
      final Gradient gradient = SweepGradient(
        startAngle: 0,
        endAngle: 1,
        colors: [Colors.white, Colors.white.withOpacity(0.3)],
      );
      final blockSize =
          Rect.fromCircle(center: Offset(0.w, 148.w), radius: 5.w);
      paint.shader = gradient.createShader(blockSize);
    }

    canvas.drawPath(path, paint);

    List angels = [
      0.25,
      0.4,
      0.69,
    ];

    List paints = [paint1, paint2, paint3];

    if (haveBool == false) {
      //当前点位置
      Paint currentPaint = Paint();

      currentPaint.style = PaintingStyle.stroke;
      currentPaint.color = HexColor("#FFA900");
      currentPaint.strokeWidth = 6.w;
      drawCircle(paint: currentPaint, radiu: 12.w, ang: angle);

      currentPaint.style = PaintingStyle.fill;
      currentPaint.color = Colors.white;
      drawCircle(paint: currentPaint, radiu: 8.w, ang: angle);

      for (int i = 0; i < 3; i++) {
        drawCircle(paint: paints[i], radiu: 9.5.w, ang: angels[i]);
      }
    } else {
      if (score < 40) {
        for (int i = 0; i < 3; i++) {
          drawCircle(paint: paints[i], radiu: 9.5.w, ang: angels[i]);
        }
      } else if (score < 60) {
        drawCircle(paint: paints[1], radiu: 9.5.w, ang: angels[1]);
        drawCircle(paint: paints[2], radiu: 9.5.w, ang: angels[2]);
      } else if (score < 85) {
        drawCircle(paint: paints[2], radiu: 9.5.w, ang: angels[2]);
      }
    }
  }

  ///你的绘画依赖于一个变量并且该变量发生了变化，那么你在这里返回true，
  ///这样Flutter就知道它必须调用paint方法来重绘你的绘画。否则，在此处返回false表示您不需要重绘
  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

class HexColor extends Color {
  static int _getColorFromHex(String hexColor) {
    hexColor = hexColor.toUpperCase().replaceAll("#", "");
    if (hexColor.length == 6) {
      hexColor = "FF" + hexColor;
    }
    return int.parse(hexColor, radix: 16);
  }

  HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
}
